package com.newear.happyDialog;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;

import com.newear.R;
import com.newear.base.calendar.Day;
import com.newear.base.calendar.MyCalendarView;

import java.util.Calendar;

/**
 * Created by happy on 2018/6/1.
 */

public class HistoryDateDialog extends AlertDialog {
    private Context mContext;
    private MyCalendarView cv_history_events;

    public HistoryDateDialog(Context context, int themeResId) {
        super(context, themeResId);
        mContext = context;
        //点击外面不允许消失,包括返回键
        setCanceledOnTouchOutside(false);
        getWindow().getAttributes().gravity = Gravity.CENTER;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history_data_popup_layout);
        init();
        findViews();
        setListeners();
    }

    private void init(){

    }

    private void findViews(){
        //日历控件
        cv_history_events = (MyCalendarView) findViewById(R.id.cv_history_events);
    }

    private void setListeners(){
        cv_history_events.setOnDayClickListener(new MyCalendarView.OnDayClickListener() {
            @Override
            public void onDayClicked(AdapterView<?> adapter, View view, int position, long id, Day day) {
                HistoryDateDialog.this.dismiss();
                Calendar calendar = cv_history_events.getCalendar();
                if (calendar != null) {
                    int year = day.getYear();
                    int month = day.getMonth() + 1;
                    int now = day.getDay();
                    if (onSelectedListener != null) {
                        onSelectedListener.onDayClicked(year, month, now);
                    }
                }
            }
        });
    }

    private OnSelectedListener onSelectedListener;
    public void setOnSelectedListener(OnSelectedListener onSelectedListener) {
        this.onSelectedListener = onSelectedListener;
    }

    public interface OnSelectedListener{
        void onDayClicked(int year, int month, int day);
    }
}
